package cn.mars4j.nutch.web.parser;

import java.util.List;
import java.util.Map;

import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.client.params.HttpClientParams;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.AbstractHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;


public class CnkiParser extends BaseParser {

	/**
	 * CNKI分类数据库_期刊
	 */
	public static final String DBPREFIX_CNKI = "dbPrefix";

	/**
	 * 构造函数
	 * @param context
	 */
	public CnkiParser(Map<Object, Object> context) {
		super("cnki.xml");
		this.context.putAll(context);

		// 分类
		String classify = (String)context.get(Parser.CLASSIFY);
		
		if ("QK".equals(classify)) {
			this.context.put(DBPREFIX_CNKI, "SCDB");
		} else if ("HY".equals(classify)) {
			this.context.put(DBPREFIX_CNKI, "CIPD");
		} else {
			this.context.put(DBPREFIX_CNKI, "SCDB");
		}
	}
	
	/**
	 * 设置配置文件参数
	 * @param context
	 */
	protected Map<Object, Object> getContext() {
		
		try {
			this.context.put("cookie", getCookie((String)this.context.get(Parser.MKEY), (String)this.context.get(DBPREFIX_CNKI)));
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return this.context;
	}
	
	/**
	 * 获取CNKIcookie信息
	 */
	public String getCookie(String mkey, String dbName) throws Exception {
		HttpClient httpClient = new DefaultHttpClient();  
        HttpClientParams.setCookiePolicy(httpClient.getParams(), CookiePolicy.BROWSER_COMPATIBILITY);    
        HttpHost httpHost = new HttpHost("epub.cnki.net");  
        HttpGet httpGet = new HttpGet("/KNS/request/SearchHandler.ashx?PageName=ASP.brief_default_result_aspx&DbPrefix="+ dbName +"&txt_1_sel=FT%24%25%3D%7C&txt_1_value1=" + mkey + "&parentdb=" + dbName);
        httpGet.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
        httpGet.setHeader("Accept-Language", "zh-CN,zh;q=0.8,en;q=0.6");
        httpGet.setHeader("Cache-Control", "no-cache");
        httpGet.setHeader("Connection", "keep-alive");
        httpGet.setHeader("Host", "epub.cnki.net");
        httpGet.setHeader("Pragma", "no-cache");
        httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36");
          
        HttpResponse response = httpClient.execute(httpHost,httpGet); 
        
        String sessionId = null;
        String sid = null;
        
        //读取cookie并保存文件  
        List<Cookie> cookies = ((AbstractHttpClient) httpClient).getCookieStore().getCookies();    
        if (cookies.isEmpty()) {    
            System.out.println("None");    
        } else {    
            for (int i = 0; i < cookies.size(); i++) {
                if ("ASP.NET_SessionId".equals(cookies.get(i).getName())) {
                	sessionId = cookies.get(i).getValue();
                }
                if ("SID_kns".equals(cookies.get(i).getName())) {
                	sid = cookies.get(i).getValue();
                }
            }    
        } 
        return "ASP.NET_SessionId=" + sessionId + ";" + "SID_kns=" + sid + ";";
	}

}
